//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.base.domain.*;
import com.ruoyi.live.base.mapper.LiveExamObjectiveScoreDetailMapper;
import com.ruoyi.live.base.mapper.LiveProductSourceMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserDetailMapper;
import com.ruoyi.live.standard.service.CountResult;
import com.ruoyi.live.standard.service.StandardService;
import com.ruoyi.live.tool.commons.IConvert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class OneStandardEduImpl implements StandardService {
    private static final Logger log = LoggerFactory.getLogger(OneStandardEduImpl.class);
    @Autowired
    LiveProductSourceMapper sourceDao;
    @Autowired
    LiveTaskUserDetailMapper detailDao;
    @Autowired
    CacheService cacheService;
    @Autowired
    LiveExamObjectiveScoreDetailMapper scoreDetailDao;
    @Autowired
    JdbcTemplate jdbcTemplate;

    public OneStandardEduImpl() {
    }

    public String getStandardCode() {
        return "SCORE_RULE_PRODUCT_ONE_EDU";
    }

    public CountResult doCountScore(LiveExamUser examUser, Long scoreId) {
        LiveScoreStandard standard = this.cacheService.getStandardByCode(this.getStandardCode());
        List<LiveTaskUser> tasks = examUser.getTaskUserList();
        Iterator var5 = tasks.iterator();

        while(var5.hasNext()) {
            LiveTaskUser task = (LiveTaskUser)var5.next();
            Boolean taskCorrectFlag = false;
            if (task.getTaskType() == 1) {
                LiveExamObjectiveScoreDetail scoreDetail = (new LiveExamObjectiveScoreDetail()).setTaskUserId(task.getId()).setScoreId(scoreId).setState(1).setProductId(task.getProductId());
                if (standard == null) {
                    scoreDetail.setState(2).setErrDesc("获取评分标准失败,code为SCORE_RULE_PRODUCT_ONE");
                }

                scoreDetail.setStandardId(standard.getId()).setStandardName(standard.getStandardName());

                try {
                    List<LiveTaskUserDetail> details = this.detailDao.selectList(new QueryWrapper((new LiveTaskUserDetail()).setTaskUserId(task.getId()).setDetailOrder(1).setDetailType(1)));
                    if (details.size() > 0) {
                        LiveTaskUserDetail detail = (LiveTaskUserDetail)details.get(0);
                        Integer picCountOK = IConvert.object2int(this.cacheService.getParam("SYSTEM_PRODUCT_SOURCE_COUNT"));
                        String ifComputeCorrect = this.cacheService.getParam("SYSTEM_COMPUTE_CORRECT");
                        LiveProductSource source = (LiveProductSource)this.sourceDao.selectOne(new QueryWrapper((new LiveProductSource()).setProductId(task.getProductId()).setFirst(1)));
                        if (source.getId().equals(detail.getProductSourceId())) {
                            Long count = this.detailDao.selectCount(new QueryWrapper((new LiveTaskUserDetail()).setTaskUserId(task.getId()).setDetailType(1)));
                            if (count >= picCountOK) {
                                if ("1".equals(ifComputeCorrect)) {
                                    taskCorrectFlag = true;
                                } else {
                                    String sql = "select count(1) count, sum(answer) answer from live_product_source  where id in (select a.product_source_id from live_task_user_detail a where a.task_user_id = ? and a.detail_type = 1)";
                                    List<Map<String, Object>> res = this.jdbcTemplate.queryForList(sql, new Object[]{task.getId()});
                                    if (res.size() <= 0) {
                                        throw new Exception("计算得分出错");
                                    }

                                    Integer correctCount = IConvert.object2int(((Map)res.get(0)).get("count"));
                                    Integer answer = IConvert.object2int(((Map)res.get(0)).get("answer"));
                                    if (correctCount >= IConvert.object2int(this.cacheService.getParam("SYSTEM_PRODUCT_SOURCE_COUNT")) && correctCount.equals(answer)) {
                                        taskCorrectFlag = true;
                                    }
                                }
                            }
                        }

                        if (taskCorrectFlag) {
                            scoreDetail.setHasScore(1).setScore(standard.getStandardValue());
                        } else {
                            scoreDetail.setHasScore(2).setScore(new BigDecimal(0));
                        }

                        this.scoreDetailDao.insert(scoreDetail);
                    } else {
                        scoreDetail.setHasScore(2).setState(1).setScore(new BigDecimal(0));
                        this.scoreDetailDao.insert(scoreDetail);
                    }
                } catch (Exception var19) {
                    var19.printStackTrace();
                    log.error("LiveExamUser的id为{}计算得分出错，code为SCORE_RULE_PRODUCT_ONE", examUser.getId(), var19);
                    scoreDetail.setState(2).setErrDesc("计算得分出错，code为SCORE_RULE_PRODUCT_ONE");
                    this.scoreDetailDao.insert(scoreDetail);
                }
            }
        }

        return new CountResult(0.0D);
    }
}
